
	// easyUI 扩展接口
	var easyUI = {};
	easyUI.extend = {};
		
	/**
	 * -------------------表格单元格编辑----------------------
	 * 
	 * @param {String} tableSelector --> table的选择器
	 */
	easyUI.extend.TABLE_CELL =  function(tableSelector){
		// 修改标识
		this.__proto__.sign = false;
		
		/**
		 * 编辑
		 */
		this.editor = function(){
			var row = $(tableSelector).datagrid('getSelected');
			var index = $(tableSelector).datagrid('getRowIndex', row);
			if (!easyUI.extend.TABLE_CELL.prototype.sign){
				$(tableSelector).datagrid('beginEdit', index);
			} else{
				$(tableSelector).datagrid('endEdit', index);
			}
		}
		
		/**
		 * 编辑-取消
		 */
		this.cancel = function(){
			var row = $(tableSelector).datagrid('getSelected');
			var index = $(tableSelector).datagrid('getRowIndex', row);
			$(tableSelector).datagrid('cancelEdit', index);
		}
		
		/**
		 * 更新表格内容
		 * @param {int} index --> 行的索引值
		 */
		this.updateActions = function(index){
			$(tableSelector).datagrid('updateRow',{
				index: index,
				row:{}
			});
		}
	};
	
	/**
	 * ---------------------弹框表格------------------------
	 * 
	 * @param {String} 	dlgTitle	--> 对话框名称
	 * @param {String} 	tableName 	--> 表格名称
	 * @param {Object} 	url 		--> url对象
	 * @param {int} 	width 		--> 对话框宽度(默认为350px)
	 * @param {int} 	height 		--> 对话框高度(默认为400px)
	 */
	easyUI.extend.DLAG_TABLE = function(dlgTitle, tableName, url, width, height){
		// 表格名称
		this.tableName = tableName;
		// 表格标题
		this.dlgTitle = dlgTitle;
		// 弹框宽度
		this.width = width || 350;
		// 弹框高度
		this.height = height || 400;
		// 表格选择器
		this.tableSelector = '#' + tableName + '-dg';
		// 表格编辑
		this.tableCell = new easyUI.extend.TABLE_CELL(this.tableSelector);
		
		/**
		 * 显示对话框
		 */
		this.showDlg = function(){
			$('#' + tableName + '-dlg').dialog({
		        title: this.dlgTitle,
		        width: this.width,
		        closed: false,
		        cache: false,
		        top: ($(document).height() - this.height) * 0.5,
		        left: ($(window).width() - this.width) * 0.5,
		        modal: true,
		    });
		};
		
		/**
		 * 关闭对话框
		 */
		this.closeDlg = function(){
			$('#' + tableName + '-dlg').dialog('close');
		}
		
		/**
		 * 修改-保存
		 */
		this.updateSave = function(){
			this.save(url.update);
		};
		
		/**
		 * 新增-编辑
		 */
		this.insert = function(){
			$(this.tableSelector).datagrid('insertRow', {index: 0, row: {operation: -1}});
			$(this.tableSelector).datagrid('beginEdit', 0);
		};

		/**
		 * 新增-保存
		 */
		this.insertSave = function(){
			this.save(url.insert);
		};
		
		/**
		 * 保存
		 * @param {String} url --> url地址
		 */
		this.save = function(url){
			var clazz = this;
			var isValid = true;
			var row = $(this.tableSelector).datagrid('getSelected');
			// 获得所有编辑器
			var ed = $(this.tableSelector).datagrid('getEditors', $(this.tableSelector).datagrid('getRowIndex', row));
			// 遍历编辑器
			for (var one in ed){
				row[ed[one].field] = ed[one].target.val();
				if (!eval('$(ed[one].target).' + ed[one].type + '(\'isValid\')')){
					isValid = false;
				}
			}
			if (isValid){
				$.ajax({
			        type: 'POST',
			        url: url, 
			        dataType: 'json',
			        async: false,
			        data: row,
			        success: function (rs) {
						if (rs.status == 0){
	                    	$.messager.alert("系统提示", rs.msg);
	                    	clazz.tableCell.editor();
	                    	$(clazz.tableSelector).datagrid('reload');
	                    } else {
	                        $.messager.show({
	                            title:'错误提示' + rs.status,
	                            msg:rs.msg
	                        })
	                    }
			        }
			    });
		    } else {
		   		$.messager.alert("系统提示","数据不完整");
		    }
		}
		
		/**
		 * 新增-取消
		 */
		this.insertCancel = function(){
			this.tableCell.cancel();
			$(this.tableSelector).datagrid('deleteRow', 0);
		};
		
		/**
		 * 删除
		 */
		this.del = function(){
			var clazz = this;
			var row = $(this.tableSelector).datagrid('getSelected');
			if(row){
				$.messager.confirm("系统提示",'确定需要删除-' + row.name + '-这条信息吗？',function(r){
		            if(r){
		                $.ajax({
		                    type: 'POST',
		                    url: url.del,
		                    async: false,
		                    dataType: 'json',
		                    data: {id : row.id},
		                    success: function (rs) {
		                        if (rs.status == 0){
		                        	$.messager.alert("系统提示", rs.msg);
		                        	$(clazz.tableSelector).datagrid('reload');
		                        } else {
		                            $.messager.show({
		                                title:'错误提示' + rs.status,
		                                msg:rs.msg
		                            })
		                        }
		                    }
		                });
		            }
		        });
			}
		};
	}
	
	/**
	 * ---------------------表格------------------------
	 * 
	 * @param {String} tableName	 --> 表格名称
	 * @param {Object} url 			 --> url对象
	 */
	easyUI.extend.TABLE = function(tableName, url){
		// 表格选择器
		this.tableSelector = '#' + tableName + '-dg';

		/**
		 * @param {String} title --> 对话框标题
		 */
		this.showDlg = function(title){
			$('#' + tableName + '-dlg').dialog({
		        title: title,
		        closed: false,
		        cache: false,
		        modal: true,
		        resize: {},
		        onClose: function(){
		        	$('#' + tableName + '-tb>form').form('reset');
		        }
			});
		}
		
		/**
		 * 新增-信息记录
		 * @param {String} title		--> 对话框标题
		 * @param {Object} formRule		--> 表单显示规则
		 */
		this.insert = function(title, formRule){
			formRule && formRule();
			this.showDlg(title);
			$('#' + tableName + '-dlg>form').form('reset');	
			$('#' + tableName + '-dlg>form input[name="id"]').val(0);
			
		};
		
		/**
		 * 修改--信息记录
		 * @param {String} title		--> 对话框标题
		 * @param {Object} formRule		--> 表单显示规则
		 */
		this.update = function(title, formRule, dataProcess){
			var row = $(this.tableSelector).datagrid('getSelected');
			if(row){
				formRule && formRule();
				this.showDlg(title);
				$('#' + tableName + '-dlg>form').form('load', row);
//				$('#' + tableName + '-dlg>form input[name="id"]').val(1);
				dataProcess && dataProcess(row);
			}
		};
		
		/**
		 * 保存--信息(新增、修改)
		 */
		this.save = function(){
			var searchData = new FormData($('#' + tableName + '-dlg>form')[0]);
			$('#' + tableName + '-dlg>form').form('submit',{
				url: url.save, 
		        onSubmit:function(){
		            var isValid=$(this).form('validate');
		            if(!isValid){
		                $.messager.alert("系统提示","数据不完整");
		            } 
		            return isValid;
		        },
		        success: function(rs){
		        	rs = eval('(' + rs + ')');
			    	if (rs.status == 0){
                    	$.messager.alert("系统提示", rs.msg);
                    	$('#' + tableName + '-dlg').dialog('close')
                    	$('#' + tableName + '-dg').datagrid('reload')
                    } else {
                        $.messager.show({
                            title:'错误提示' + rs.status,
                            msg:rs.msg
                        })
                    }
		        }
		    });
		};
		
		/**
		 * 删除--信息记录
		 * @param {String} message --> 消息提示信息
		 */
		this.del = function(message){
			var clazz = this;
			var row = $(this.tableSelector).datagrid('getSelected');
			if(row){
				$.messager.confirm("系统提示", message, function(r){
		            if(r){
		                $.ajax({
		                    type: 'POST',
		                    url: url.del,
		                    async: false,
		                    dataType: "json",
		                    data: row,
		                    success: function (rs) {
		                        if (rs.status == 0){
		                            $.messager.alert("系统提示", rs.msg);
		                            $(clazz.tableSelector).datagrid('reload');
		                        } else {
		                            $.messager.show({
		                                title:'错误提示' + rs.status,
		                                msg:rs.msg
		                            })
		                        }
		                    }
		                });
		            }
		        });
			}
		};
		
		/**
		 * 批量删除--信息记录
		 */
		this.batchDel = function(){
			var clazz = this;
			var rows = $(this.tableSelector).datagrid('getChecked');
			var ids = "";
			for (var one in rows){
				ids += rows[one].id + ",";
			}
			
			if(rows.length > 0){
				$.messager.prompt("系统提示", '请输入您的密码进行验证！', function(pw){
		            if (pw){
		                $.ajax({
		                    type: 'POST',
		                    url: url.batchDel,
		                    async: false,
		                    dataType: "json",
		                    data: {ids: ids, password: pw},
		                    success: function (rs) {
		                        if (rs.status == 0){
		                            $.messager.alert("系统提示", rs.msg);
		                            $(clazz.tableSelector).datagrid('reload');
		                        } else {
		                            $.messager.show({
		                                title:'错误提示' + rs.status,
		                                msg:rs.msg
		                            })
		                        }
		                    }
		                });
		            } else {
		            	$.messager.show({title:'温馨提示！', msg: '密码不能为空'})
		            }
		        });
		        $('.messager-input').attr('type', 'password');
			} else {
				$.messager.alert("系统提示", "请选择要删除的记录！！");
			}
			
		};
		
		/**
		 * 高级查询-加载
		 */
		this.query = function(){
			var searchData = new FormData($('#' + tableName + '-tb>form')[0]);
			var iterator = searchData.entries();
			var searchObject = {};
			var value;
			do{
				value = iterator.next().value;
				if (value)
					searchObject[value[0]] = value[1];
			}while(value);
			$(this.tableSelector).datagrid('reload', searchObject);
		}
		
		/**
		 * 高级查询-重置
		 */
		this.reset = function(){
			$('#' + tableName + '-tb>form').form('reset');
		}
	}
	
	/**
	 * ---------------------子表------------------------
	 * @param {String} tableSelector --> 表格选择器
	 * @param {Object} url			 --> url对象
	 */
	easyUI.extend.TABLE_SUB =  function(tableSelector, url){
		
		// 继承TABLE_CELL属性
		this.tableCell = new easyUI.extend.TABLE_CELL(tableSelector);
		// 继承TABLE属性
		this.__proto__ = new easyUI.extend.TABLE(null, url);
		
		/**
		 *  @overview TABLE tableSelector属性
		 */
		this.__proto__.tableSelector = tableSelector;
		
		/**
		 * @overview TABLE save方法
		 */
		this.__proto__.insert = null;
		
		/**
		 * @overview TABLE update方法
		 */
		this.__proto__.update = function(){
			this.__proto__.save(url.update, this);
		};
		
		/**
		 * @extends DLAG_TABLE save方法
		 */
		this.__proto__.save = function(url, clazz){
			var isValid = true;
			var row = $(this.tableSelector).datagrid('getSelected');
			// 获得所有编辑器
			var ed = $(this.tableSelector).datagrid('getEditors', $(this.tableSelector).datagrid('getRowIndex', row));
			// 遍历编辑器
			for (var one in ed){
				row[ed[one].field] = eval('$(ed[one].target).' + ed[one].type + '(\'getValue\')');
				if (!eval('$(ed[one].target).' + ed[one].type + '(\'isValid\')')){
					isValid = false;
				}
			}
			if (isValid){
				$.ajax({
			        type: 'POST',
			        url: url, 
			        dataType: 'json',
			        async: false,
			        data: row,
			        success: function (rs) {
						if (rs.status == 0){
	                    	$.messager.alert("系统提示", rs.msg);
	                    	clazz.tableCell.editor();
	                    	$(clazz.tableSelector).datagrid('reload');
	                    } else {
	                        $.messager.show({
	                            title:'错误提示' + rs.status,
	                            msg:rs.msg
	                        })
	                    }
			        }
			    });
		    } else {
		   		$.messager.alert("系统提示","数据不完整");
		    };
		};
		
		/**
		 * @overview TABLE query方法
		 */
		this.__proto__.query = null;
		
		/**
		 * @overview TABLE reset方法
		 */
		this.__proto__.reset = null;
		
		/**
		 * @overview TABLE showDlg方法
		 */
		this.__proto__.showDlg = null;
	}
	

	/**
	 * ------------------组合框初始化----------------------
	 * @param {String} selector	--> 组合框选择器
	 * @param {String} url		--> url地址
	 * 
	 */
	easyUI.extend.Combobox =  function(selector, url){
		
		/**
		 *  combobox初始化
		 */
		this.init = function(){
			$(selector).combobox({
				url: url,    
			    valueField: 'id',    
			    textField: 'name',
			    editable: false,
			});
		};
		
		/**
		 *  combobox
		 */
		this.reload = function(){
			$(selector).combobox('reload');
		};
	}
	
	// 添加时间格式原型
	Date.prototype.format = function(format) {
	    var date = {
	        "M+": this.getMonth() + 1,
	        "d+": this.getDate(),
	        "h+": this.getHours(),
	        "m+": this.getMinutes(),
	        "s+": this.getSeconds(),
	        "q+": Math.floor((this.getMonth() + 3) / 3),
	        "S+": this.getMilliseconds()
	    };
	    if (/(y+)/i.test(format)) {
	        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
	    }
	    for (var k in date) {
	        if (new RegExp("(" + k + ")").test(format)) {
	            format = format.replace(RegExp.$1, RegExp.$1.length == 1
	                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
	        }
	    }
	    return format;
	}
	
	// 时间戳转字符串
	function timeStampToStr(timeStamp){
		if(!timeStamp)return '';
	    var newDate = new Date();
	    newDate.setTime(timeStamp);
	    
	    return newDate.format('yyyy-MM-dd hh:mm:ss')
	}